/**
 * 
 */
package org.yi.fc.entity;

import java.util.ArrayList;
import java.util.List;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author ilgqh
 * 文章类型实体
 */
public class FContentType extends Model<FContentType> {
	private static final long serialVersionUID = -4802325659453281172L;
	public static final FContentType dao = new FContentType();
	
	/**
	 * 根据文章类型的中文名称查找数据
	 * @param typeNameCN
	 * @return
	 */
	public List<FContentType> queryByTypeNameCN(String typeNameCN){
		Preconditions.checkNotNull(Strings.emptyToNull(typeNameCN), "保存文章类型时类型名称不允许为空!");
		
		String sql = "select * from f_content_type where type_name_cn = ?";
		List<FContentType> list = this.find(sql, typeNameCN);
		return list;
	}
	
	/**
	 * 根据条件查询文章类型
	 * @param typeNameCN			类型中文名
	 * @param typeNameEN			类型英文名
	 * @param typeStatus			类型状态
	 * @param userId				创建该类型的用户的id
	 * @return List<FContentType>
	 */
	public List<FContentType> queryContentType(String typeNameCN,String typeNameEN,String typeStatus,Long userId ){
		List<FContentType> list = new ArrayList<FContentType>();
		StringBuffer sb = new StringBuffer("select * from f_content_type where 1=1 ");
		if(!Strings.isNullOrEmpty(typeNameCN)){
			sb.append(" and type_name_cn = '").append(typeNameCN).append("'");
		}
		
		if(!Strings.isNullOrEmpty(typeNameEN)){
			sb.append(" and type_name_en = '").append(typeNameEN).append("'");
		}
		
		if(!Strings.isNullOrEmpty(typeStatus)){
			sb.append(" and type_status = '").append(typeStatus).append("'");
		}
		
		if(userId != null && userId > 0){
			sb.append(" and user_id = ").append(userId).append("");
		}
		list = this.find(sb.toString());
		return list;
	}
	
	/**
	 * 根据文章类型的中文名称判断该文章类型是否存在
	 * @param typeNameCN
	 * @return
	 */
	public boolean isTypeNameExisit(){
		List<FContentType> list = queryByTypeNameCN(this.getStr("type_name_cn"));
		return list == null ? false : (list.size() > 0 ? true : false);
	}
	
	/**
	 * 查询文章类型
	 * @return
	 */
	public Page<FContentType> queryContentType(int pageNumber, int pageSize){
		
		StringBuilder sb = new StringBuilder("from f_content_type ");
		sb.append(" order by create_time desc");
		
		return paginate(pageNumber, pageSize, "select * ", sb.toString());
	}
	
}
